<?php 
namespace app\superadmin\controller;
use app\superadmin\model\Index as IndexModel;
//继承Base控制器
class Index extends Base
{
	//显示首页轮播图列表
    public function index()
    {
    	$res= \think\Db::name('slideshow')
             ->select();
        $this->assign('isfirst',$res);
    	return $this->fetch();
    }

    //添加首页轮播图
    public function addslideshow()
    {
    	if(request()->isPost()){
    		$data=[
    		  'shop_id'=>input('shop_id'),
    		  'remark'=>input('remark'),
    		  'up'=>input('up'),
    		  'createtime'=>date('Y-m-d H:m:s'),
    		];
    		//上传图片
    		if($_FILES['picture']['tmp_name']){
    		 // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file('picture');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'static/admin/Uploads/slideshow');
            if($info){
              $data['picture']=date('Ymd').'/'.$info->getFilename();
     
             }else{
               // 上传失败获取错误信息
               $this->error($file->getError());
             }
        }

           //添加数据前应该先验证
    		$validate = \think\Loader::validate('Slideshowval');

    		if($validate->check($data)){
    		$db=\think\Db::name('slideshow')->insert($data);
    		if($db){
    			return $this->success('首页轮播图添加成功！','index');
    		}
    		else{
    			return $this->error('添加失败！','index');
    		 }
    		}else{
    			$this->error($validate->getError());
    		}
    		return;
    	}

    }


     //置顶轮播图片
     public function top(){
 	  $s_id=input('s_id');//获取id
      if($db=\think\Db::name('slideshow')->where('s_id',$s_id)->update(['up'=>1])){
         //return $this->success('餐厅状态重置成功！','dininglist');
         return $this->redirect('index');
      }else{
         return $this->error('餐厅置顶失败！','index');
      }
       return;
    } 


     //取消置顶轮播图片
     public function cancel(){
 	  $s_id=input('s_id');//获取id
      if($db=\think\Db::name('slideshow')->where('s_id',$s_id)->update(['up'=>0])){
         //return $this->success('餐厅状态重置成功！','dininglist');
         return $this->redirect('index');
      }else{
         return $this->error('餐厅取消置顶失败！','index');
      }
       return;
    } 
    

    //删除首页轮播图
    public function delete(){
      $s_id=input('s_id');
      $res= \think\Db::name('slideshow')->where('s_id',$s_id)->find();
      $picture=$res['picture'];
      $file=APP.'public/static/admin/Uploads/slideshow/'.$picture;
      // var_dump($file);die;
     
      if(db('slideshow')->delete($s_id)){
        unlink($file);
        
        return $this->redirect('index');

      }else{
        return $this->error('删除失败！','index');
      }
    }


    
 


   


}
